﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.users
{
    public partial class cur_realation : Web.UI.ManagePage
    {
        Model.manager mgr;
        string appWhere="";
        private int id = 0;
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        protected string keywords = string.Empty;
        protected string realation_id;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            this.id = DTRequest.GetQueryInt("id");
            this.keywords = DTRequest.GetQueryString("keywords");
            this.realation_id = DTRequest.GetQueryString("realation_id");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();
                if (!new BLL.manager().Exists(this.id))
                {
                    JscriptMsg("邀请码不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                if (Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager == null)
                {
                    Response.Redirect("/admin/login.aspx");
                }
                mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;

                //&&显示名称
                LabelBind();
                RptBind();
            }
        }

        private void LabelBind()
        {
            DataSet ds = new BLL.sf_manager_scale().GetList(Utils.getWhereByInfo(mgr.busnieseNum.ToString(), Session[DTKeys.SESSION_APP_INFO].ToString(), Session[DTKeys.SESSION_TEMPLATES_INFO].ToString()));
            if (ds != null)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (ds.Tables[0].Rows[i]["levels"].ToString().Equals("1"))
                    {
                        lbl1.Text = ds.Tables[0].Rows[i]["levelName"].ToString();
                    }
                    if (ds.Tables[0].Rows[i]["levels"].ToString().Equals("2"))
                    {
                        lbl2.Text = ds.Tables[0].Rows[i]["levelName"].ToString();
                    }
                    if (ds.Tables[0].Rows[i]["levels"].ToString().Equals("3"))
                    {
                        lbl3.Text = ds.Tables[0].Rows[i]["levelName"].ToString();
                    }
                }
            }
        }

        //数据绑定
        private void RptBind()
        {
            this.ddlReation.SelectedValue = this.realation_id;
            BLL.sf_user_info bll = new BLL.sf_user_info();
            Model.sf_user_info model = bll.GetModel(this.id);
            appWhere = Utils.getWhereByInfo(model.busnieseNum, model.appNum, model.templatesNum);
            if (String.IsNullOrEmpty(this.realation_id))
                this.realation_id = "0";
            if (model != null)
            {
                lblCurCustomer.Text = model.nickName;
                DataSet ds = bll.GetList(" fatherKey='" + model.inviteKey + "' and "+appWhere);
                if (ds != null)
                {
                    StringBuilder str = new StringBuilder();
                    str.Append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"ltable\">");
                    str.Append("<tr>");
                    str.Append("<th align=\"left\" width=\"10%\" style='padding-left:5px'>用户编号</th>");
                    str.Append("<th align=\"left\" width=\"10%\">微信昵称</th>");
                    str.Append("<th align=\"left\" width=\"10%\">邀请码</th>");
                    str.Append("<th align=\"left\" width=\"10%\">注册时间</th>");
                    str.Append("</tr>");
                    DataTable dt = ds.Tables[0];
                    #region 内部层级计算
                    //&&遍历第一级名称
                    for (int ii = 0; ii < dt.Rows.Count; ii++)
                    {
                        String inviteKey = dt.Rows[ii]["inviteKey"].ToString();
                        if (this.realation_id.Equals("1") || this.realation_id.Equals("0"))
                        {
                            str.Append("<tr>");
                            str.AppendFormat("<td style='padding-left:5px'><img src=\"../../images/imgs/one.png\"  style=\"width:22px;height:22px;\" /> {0}</td>", dt.Rows[ii]["userNum"]);
                            str.AppendFormat("<td>{0}</td>", dt.Rows[ii]["nickName"]);
                            str.AppendFormat("<td>{0}</td>", inviteKey);
                            str.AppendFormat("<td>{0}</td>", dt.Rows[ii]["addTime"]);
                            str.Append("</tr>");
                        }
                        if (this.realation_id.Equals("0") || this.realation_id.Equals("2") || this.realation_id.Equals("3"))
                        {
                            #region 第二级
                            DataSet ds2 = bll.GetList(" fatherKey='" + inviteKey + "' and " + appWhere);
                            if (ds2 != null)
                            {
                                DataTable dt2 = ds2.Tables[0];
                                for (int jj = 0; jj < dt2.Rows.Count; jj++)
                                {
                                    String inviteKey2 = dt2.Rows[jj]["inviteKey"].ToString();
                                    if (this.realation_id.Equals("2") || this.realation_id.Equals("0"))
                                    {
                                        str.Append("<tr>");
                                        str.AppendFormat("<td style='padding-left:5px'><img src=\"../../images/imgs/two.png\" style=\"width:22px;height:22px;\" />  {0}</td>", dt2.Rows[jj]["userNum"]);
                                        str.AppendFormat("<td>{0}</td>", dt2.Rows[jj]["nickName"]);
                                        str.AppendFormat("<td>{0}</td>", inviteKey2);
                                        str.AppendFormat("<td>{0}</td>", dt2.Rows[jj]["addTime"]);
                                        str.Append("</tr>");
                                    }
                                    if (this.realation_id.Equals("3") || this.realation_id.Equals("0"))
                                    {
                                        #region 第三级
                                        DataSet ds3 = bll.GetList(" fatherKey='" + inviteKey2 + "' and " + appWhere);
                                        if (ds3 != null)
                                        {
                                            DataTable dt3 = ds3.Tables[0];
                                            for (int kk = 0; kk < dt3.Rows.Count; kk++)
                                            {
                                                str.Append("<tr>");
                                                str.AppendFormat("<td style='padding-left:5px'><img src=\"../../images/imgs/three.png\"  style=\"width:22px;height:22px;\" />  {0}</td>", dt3.Rows[kk]["userNum"]);
                                                str.AppendFormat("<td>{0}</td>", dt3.Rows[kk]["nickName"]);
                                                str.AppendFormat("<td>{0}</td>", dt3.Rows[kk]["inviteKey"]);
                                                str.AppendFormat("<td>{0}</td>", dt3.Rows[kk]["addTime"]);
                                                str.Append("</tr>");
                                            }
                                        }
                                        #endregion
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                    #endregion

                    if (dt.Rows.Count == 0)
                    {
                        str.Append("<tr><td align=\"center\" colspan=\"8\">此人推广能力可见一般</td></tr>");
                    }
                    str.Append("</table>");

                    showList.InnerHtml = str.ToString();
                }
            }
        }

        #region 组合SQL查询语句
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (userNum like  '%")
                      .Append(_keywords)
                      .Append("%' or inviteKey like '%")
                      .Append(_keywords)
                      .Append("%' or nickName like '%")
                      .Append(_keywords)
                      .Append("')");
            }
            return strTemp.ToString();
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("cur_realation.aspx", "realation_id={0}&keywords={1}&id={2}",
                 ddlReation.SelectedValue, this.keywords, this.id.ToString()));
        }

        //&&关系选择
        protected void ddlReation_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("cur_realation.aspx", "realation_id={0}&keywords={1}&id={2}",
                 ddlReation.SelectedValue, this.keywords, this.id.ToString()));
        }
    }
}